package com.eclab.springbootgo.service.impl;

import com.eclab.springbootgo.model.User;
import com.eclab.springbootgo.repository.UserRepository;
import com.eclab.springbootgo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户业务类
 *
 * @author Aiden <a href="mailto:adam602260@gmail.com"/>
 * @version UserServiceImpl.java v0.1
 * @create 2019-08-27 15:12
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 保存一个用户
     *
     * @param user
     */
    @Override
    public void save(User user) {
        if (user == null){
            throw new RuntimeException("user can not be null");
        }else if (StringUtils.isEmpty(user.getUsername()) ||
                         StringUtils.isEmpty(user.getPassword())){
            throw new RuntimeException("username or password can not be null");
        }
        userRepository.save(user);
    }

    /**
     * 根据username查询一个用户
     *
     * @param username
     * @return
     */
    @Override
    public User getUser(String username) {
        if (StringUtils.isEmpty(username)){
            throw new RuntimeException("id can not be null");
        }
        return userRepository.findUserByUsername(username);
    }
}